﻿using System; 
using UniMana.Utils.Helper;
using DevExpress.XtraEditors;
using UniMana.Data;
using UniMana.Utils.Controls;
using UniMana.Utils.Data;
using System.Data;
using System.Collections.Generic;
namespace UniMana.Win.Training
{
    public partial class FrmSchedule_SubChooseSubject : DevExpress.XtraEditors.XtraForm
    {
        #region Khai Báo Biến Toàn Cục
        private bds m_bds = new bds();
        private DBMain m_DBMain = new DBMain();
        private Training_TrainingProgramInfo m_tpI;
        private int m_iSubjectKey = 0;
        private string m_strSubjectName;
        private eTeachStyle m_eTeachStyle;
        private double m_dCreadit;
        
        private List<int?> m_ilistSubjectKey = new List<int?>();

        private bool status = false;
        private string newName = "";
        private bool 
            check_lt = true, 
            check_thh = true, 
            check_bt = true, 
            check_da = true, 
            check_th = true;
        private string 
            str_lt = "", 
            str_thh = "", 
            str_bt = "", 
            str_da = "", 
            str_th = "";
        #endregion

        #region Hàm Khởi Tạo
        public FrmSchedule_SubChooseSubject()
        {
            InitializeComponent();
            ShowInTaskbar = false;
            LoadData();
        }
        private void LoadData()
        {
            lueCatalogue_Course.GetData(
                m_DBMain.m_DCReadOnly.Catalogue_Courses);
            lueCatalogue_Department.GetData(
                m_DBMain.m_DCReadOnly.Catalogue_Departments.GetDepartmentSubjectManage());
            CheckItem();
            LoadSubject();

            rluCatalogue_Course.GetData(m_DBMain.m_DCReadOnly.Catalogue_Courses);
            rluCatalogue_Department.GetData(m_DBMain.m_DCReadOnly.Catalogue_Departments);
            rluCatalogue_Level.GetData(m_DBMain.m_DCReadOnly.Catalogue_Levels);
            rluCatalogue_Major.GetData(m_DBMain.m_DCReadOnly.Catalogue_Majors);
            rluCatalogue_TrainingType.GetData(m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes);

            gclCourse.ColumnEdit = rluCatalogue_Course;
            gclDepartment.ColumnEdit = rluCatalogue_Department;
            gclLevel.ColumnEdit = rluCatalogue_Level;
            gclMajor.ColumnEdit = rluCatalogue_Major;
            gclTrainingType.ColumnEdit = rluCatalogue_TrainingType;
        }
        #endregion

        #region Khởi Tạo Biến
        public int SubjectKey
        {
            get { return m_iSubjectKey; }
        }
        public int CouserKey
        {
            get { return Cache.m_iCatalogue_CouserKey; }
            set { lueCatalogue_Course.EditValue = value; }
        }
        public int DepartmentKey
        {
            get { return Cache.m_iCatalogue_DepartmentKey; }
            set { lueCatalogue_Department.EditValue = value; }
        }
        public int MajorKey
        {
            get { return Cache.m_iCatalogue_MajorKey; }
            set { lueCatalogue_Major.EditValue = value; }
        }
        public int TrainingProgramKey
        {
            get { return Cache.m_iTraining_TrainingProgramKey; }
            set { lueTraining_TrainingProgram.EditValue = value; }
        }
        public double Credit1
        {
            get { return grvTraining_Subject.GetFocusedRowCellValue(gclCredits1).ToDouble(); }
        }
        public double Credit2
        {
            get { return grvTraining_Subject.GetFocusedRowCellValue(gclCredits2).ToDouble(); }
        }
        public double Credit3
        {
            get { return grvTraining_Subject.GetFocusedRowCellValue(gclCredits3).ToDouble(); }
        }
        public double Credit4
        {
            get { return grvTraining_Subject.GetFocusedRowCellValue(gclCredits4).ToDouble(); }
        }
        public double Credit5
        {
            get { return grvTraining_Subject.GetFocusedRowCellValue(gclCredits5).ToDouble(); }
        }
        public double TotalsCredit
        {
            get
            {
                double dTotalCredit = 0;
                double credit1 = grvTraining_Subject.GetFocusedRowCellValue(gclCredits1).ToDouble();
                double credit2 = grvTraining_Subject.GetFocusedRowCellValue(gclCredits2).ToDouble();
                double credit3 = grvTraining_Subject.GetFocusedRowCellValue(gclCredits3).ToDouble();
                double credit4 = grvTraining_Subject.GetFocusedRowCellValue(gclCredits4).ToDouble();
                double credit5 = grvTraining_Subject.GetFocusedRowCellValue(gclCredits5).ToDouble();
                if (ckeTheory.Checked) dTotalCredit += credit1;
                if (ckeExercise.Checked) dTotalCredit += credit2;
                if (ckePractice.Checked) dTotalCredit += credit3;
                if (ckeProject.Checked) dTotalCredit += credit4;
                if (ckeSelfStudy.Checked) dTotalCredit += credit5;
                return dTotalCredit;
            }
        }
        public bool CreateSubjectClass
        {
            get { return ckeCreateSubjectClass.Checked; }
        }
        public string Note
        {
            get { return txtNote.Text; }
        }
        public string Credit
        {
            get { return txtCredits.Text; }
        }
        public string Lesson
        {
            get { return txtLessons.Text; }
        }
        public bool Credit1Checked
        {
            get { return ckeTheory.Checked; }
        }
        public bool Credit2Checked
        {
            get { return ckeExercise.Checked; }
        }
        public bool Credit3Checked
        {
            get { return ckePractice.Checked; }
        }
        public bool Credit4Checked
        {
            get { return ckeProject.Checked; }
        }
        public bool Credit5Checked
        {
            get { return ckeSelfStudy.Checked; }
        }
        public string strSubjectName
        {
            get { return m_strSubjectName; }
        }
        public string strNote
        {
            get { return txtNote.Text; }
        }
        public enum eTeachStyle
        {
            Theory, Practice, Exercise, Project, ToTals
        }
        public eTeachStyle TeachStyle
        {
            get { return m_eTeachStyle; }
        }
        #endregion

        #region Sự Kiện btn
        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void btnAgree_Click(object sender, EventArgs e)
        {

        }

        private void btnAddSubjecttoProgram_Click(object sender, EventArgs e)
        {
            try
            {
                FrmTrainingProgram_SubDetail frm =
                    new FrmTrainingProgram_SubDetail();
                frm.ShowDialog();
                ShowInTaskbar = false;
                LoadData();
            }
            catch
            {
            }
        }

        private void btnAddSubject_Click(object sender, EventArgs e)
        {
            try
            {                
                FrmSubject frm = new FrmSubject();
                frm.ShowDialog();
                ShowInTaskbar = false;
                LoadData();
            }
            catch
            {
            }

        }
        #endregion

        #region Sự Kiện lue
        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            lueCatalogue_Major.GetData(
                m_DBMain.m_DCReadOnly.Catalogue_Majors.GetListByDepartMent(
                    m_DBMain.m_DCReadOnly.Catalogue_Levels,
                    lueCatalogue_Department.GetIntValue()));
            GetTrainingProgram();
        }

        private void lueCatalogue_Course_EditValueChanged(object sender, EventArgs e)
        {
            GetTrainingProgram();
        }

        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            GetTrainingProgram();
        }

        private void lueTraining_TrainingProgram_EditValueChanged(object sender, EventArgs e)
        {
            LoadSubject();
        }
        #endregion

        #region Sự Kiện grv
        private void grvTraining_Subject_DoubleClick(
            object sender, 
            EventArgs e)
        {
            if (grvTraining_Subject.FocusedRowHandle >= 0)
            {
                m_iSubjectKey =
                    grvTraining_Subject.GetFocusedRowCellValue(gclSubjectKey).ToInt();
                if (m_iSubjectKey != 0)
                {
                    #region Truyền Vào Form Con
                    status = false;

                    check_bt = ckeExercise.Checked;
                    check_bt = ckeProject.Checked;
                    check_lt = ckeTheory.Checked;
                    check_th = ckeSelfStudy.Checked;
                    check_thh = ckePractice.Checked;

                    try
                    {
                        str_lt = grvTraining_Subject.GetFocusedRowCellValue(gclCredits1).ToString();
                        str_bt = grvTraining_Subject.GetFocusedRowCellValue(gclCredits2).ToString();
                        str_thh = grvTraining_Subject.GetFocusedRowCellValue(gclCredits3).ToString();
                        str_da = grvTraining_Subject.GetFocusedRowCellValue(gclCredits4).ToString();
                        str_th = grvTraining_Subject.GetFocusedRowCellValue(gclCredits5).ToString();
                        newName = grvTraining_Subject.GetFocusedRowCellValue(gclSubjectName).ToString();
                    }
                    catch
                    {
                    }

                    Cache.m_txtCredits = TotalsCredit.ToString();
                    Cache.m_txtLessons = (TotalsCredit * 15).ToString();

                    FrmSchedule_SubChooseSubject_SubInfo frm =
                        new FrmSchedule_SubChooseSubject_SubInfo(
                            status,
                            newName,
                            check_lt, check_thh, check_bt, check_da, check_th,
                            str_lt, str_thh, str_bt, str_da, str_th,
                            ckeCreateSubjectClass.Checked,
                            txtNote.Text);
                    frm.ShowDialog();
                    frm.Close();
                    if (Cache.m_status)
                    {
                        ckeExercise.Checked = Cache.m_check_bt;
                        ckeProject.Checked = Cache.m_check_bt;
                        ckeTheory.Checked = Cache.m_check_lt;
                        ckeSelfStudy.Checked = Cache.m_check_th;
                        ckePractice.Checked = Cache.m_check_thh;

                        ckeCreateSubjectClass.Checked = Cache.m_scorede;
                        txtNote.Text = Cache.m_note;
                        txtCredits.Text = Cache.m_txtCredits;
                        txtLessons.Text = Cache.m_txtLessons;

                        grvTraining_Subject.SetFocusedRowCellValue(gclCredits1, Cache.m_str_lt);
                        grvTraining_Subject.SetFocusedRowCellValue(gclCredits2, Cache.m_str_bt);
                        grvTraining_Subject.SetFocusedRowCellValue(gclCredits3, Cache.m_str_thh);
                        grvTraining_Subject.SetFocusedRowCellValue(gclCredits4, Cache.m_str_da);
                        grvTraining_Subject.SetFocusedRowCellValue(gclCredits5, Cache.m_str_th);

                        txtSubjectName.Text = Cache.m_newName;                        
                    }
                    else
                    {
                        return;
                    }
                    #endregion

                    if (TotalsCredit > 0)
                    {
                        m_strSubjectName = txtSubjectName.Text;
                        DialogResult = System.Windows.Forms.DialogResult.OK;

                        m_dCreadit =
                            grvTraining_Subject.GetFocusedRowCellValue(
                                grvTraining_Subject.FocusedColumn).ToDouble();

                        Cache.m_iSubjectKey = m_iSubjectKey;
                        Cache.m_ilSubjectKey.Clear();
                        for (int i = 0; i < m_ilistSubjectKey.Count; i++)
                        {
                            Cache.m_ilSubjectKey.Add(m_ilistSubjectKey[i]);
                        }

                        if (grvTraining_Subject.FocusedColumn == gclCredits1)
                        {
                            m_eTeachStyle = eTeachStyle.Theory;
                        }
                        if (grvTraining_Subject.FocusedColumn == gclCredits2)
                        {
                            m_eTeachStyle = eTeachStyle.Practice;
                        }
                        if (grvTraining_Subject.FocusedColumn == gclCredits3)
                        {
                            m_eTeachStyle = eTeachStyle.Exercise;
                        }
                        if (grvTraining_Subject.FocusedColumn == gclCredits4)
                        {
                            m_eTeachStyle = eTeachStyle.Project;
                        }
                        if (grvTraining_Subject.FocusedColumn == gclTotals)
                        {
                            m_eTeachStyle = eTeachStyle.ToTals;
                        }
                        Close();
                    }
                    else
                    {
                        XtraMessageBox.Show("Không có số tín chỉ giảng dạy");
                    }
                }
                else
                {
                    XtraMessageBox.Show("Chưa chọn môn học.");
                }
            }
        }

        private void grvTraining_Subject_FocusedRowChanged(
            object sender,
            DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (grvTraining_Subject.FocusedRowHandle >= 0 &&
                grvTraining_Subject.GetFocusedRowCellValue(gclSubjectName) != null)
            {
                txtSubjectName.Text =
                    grvTraining_Subject.GetFocusedRowCellValue(
                    gclSubjectName).ToString();

                GetAnotherProgram();
                grvTraining_Program.ExpandAllGroups();

                try
                {
                    gctTraining_Class.DataSource = m_DBMain.m_DCReadOnly.Training_Classes.GetClassByProgramKey(
                                    m_DBMain.m_DCReadOnly.Training_Subject_Classes,
                                    m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details,
                                    m_DBMain.m_DCReadOnly.Training_Student_Classes,
                                    m_ilistSubjectKey);

                    CheckFilter();

                    grvTraining_Class.ExpandAllGroups();
                }
                catch
                {
                }
            }
        }

        private void grvTraining_Program_FocusedRowChanged(
            object sender, 
            DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (grvTraining_Program.FocusedRowHandle >= 0)
            {
                try
                {
                    int pr =
                        Convert.ToInt16(
                            grvTraining_Program.GetFocusedRowCellValue(
                                gclTraining_TrainingProgramKey).ToString());
                    gctTraining_Class.DataSource = m_DBMain.m_DCReadOnly.Training_Classes.GetClassByProgramKey(
                                    m_DBMain.m_DCReadOnly.Training_Subject_Classes,
                                    m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details,
                                    m_DBMain.m_DCReadOnly.Training_Student_Classes,
                                    m_ilistSubjectKey,
                                    pr);
                    grvTraining_Class.ExpandAllGroups();
                }
                catch
                {
                }
            }
            else
            {
            }
        }
        #endregion

        #region Sự Kiện rbt
        private void rbt_Status_0_CheckedChanged(object sender, EventArgs e)
        {
            CheckFilter();
        }

        private void rbt_Status_1_CheckedChanged(object sender, EventArgs e)
        {
            CheckFilter();
        }

        private void rbt_Status_All_CheckedChanged(object sender, EventArgs e)
        {
            CheckFilter();
        }

        private void rdgSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadSubject();
        }
        #endregion

        #region Hàm sữ dụng
        private void CheckFilter()
        {
            if (rbt_Status_0.Checked)
            {
                this.grvTraining_Class.ActiveFilterString = "[Status] <> True";
            }
            if (rbt_Status_1.Checked)
            {
                this.grvTraining_Class.ActiveFilterString = "[Status] = True";
            }
            if (rbt_Status_All.Checked)
            {
                this.grvTraining_Class.ActiveFilterString = "";
            }
        }

        private void GetSubject()
        {
            try
            {
                if (rdgSelect.SelectedIndex == 0)
                {
                    gctTraining_Subject.DataSource =
                        m_DBMain.m_DCReadOnly.Training_TrainingProgram_Schedules.GetByTrainingProramKey(
                            m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details,
                            m_DBMain.m_DCReadOnly.Training_Subjects,
                            m_DBMain.m_DCReadOnly.Catalogue_SubjectTypes,
                            m_DBMain.m_DCReadOnly.Training_SubjectGroups,
                            m_DBMain.m_DCReadOnly.Training_Subject_Classes,
                            m_tpI.Properties.Training_TrainingProgramKey, 
                            true);
                }
                else
                {
                    gctTraining_Subject.DataSource =
                        m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details.GetListByTrainingProgramKey(
                            m_DBMain.m_DCReadOnly.Training_Subjects,
                            m_tpI.Properties.Training_TrainingProgramKey,
                            true);
                }
            }
            catch
            {

            }
        }

        private void CheckItem()
        {
            if (lueCatalogue_Major.ItemIndex == -1)
            {
                lueCatalogue_Major.ItemIndex = 0;
            }
            if (lueCatalogue_Course.ItemIndex == -1)
            {
                lueCatalogue_Course.ItemIndex = 0;
            }
            if (lueCatalogue_Department.ItemIndex == -1)
            {
                lueCatalogue_Department.ItemIndex = 0;
            }
            if (lueTraining_TrainingProgram.ItemIndex == -1)
            {
                lueTraining_TrainingProgram.ItemIndex = 0;
            }
        }

        private void GetTrainingProgram()
        {
            int iCouser = lueCatalogue_Course.GetIntValue();
            int iMajor = lueCatalogue_Major.GetIntValue();
            if (iCouser != -1 && iMajor != -1)
            {
                lueTraining_TrainingProgram.GetData(
                    m_DBMain.m_DCReadOnly.Training_TrainingPrograms.GetListByMajorKeyCouserKey(
                        iMajor,
                        iCouser));
            }
            CheckItem();
        }

        private void LoadSubject()
        {
            try
            {
                Cache.m_iCatalogue_CouserKey = lueCatalogue_Course.GetIntValue();
                Cache.m_iCatalogue_DepartmentKey = lueCatalogue_Department.GetIntValue();
                Cache.m_iCatalogue_MajorKey = lueCatalogue_Major.GetIntValue();
                Cache.m_iTraining_TrainingProgramKey = lueTraining_TrainingProgram.GetIntValue();

                m_tpI = new Training_TrainingProgramInfo(lueTraining_TrainingProgram.GetIntValue());
                GetSubject();
                grvTraining_Subject.ExpandAllGroups();
            }
            catch
            {
            }
        }

        private void GetAnotherProgram()
        {
            #region Khởi tạo biến mới
            DataTable Program = new DataTable();
            Program.Columns.Add("Training_TrainingProgramKey");
            Program.Columns.Add("ID");
            Program.Columns.Add("TrainingProgramName");
            Program.Columns.Add("Catalogue_DepartmentKey");
            Program.Columns.Add("Catalogue_CourseKey");
            Program.Columns.Add("Catalogue_LevelKey");
            Program.Columns.Add("Catalogue_MajorKey");
            Program.Columns.Add("Catalogue_TrainingTypeKey");

            m_ilistSubjectKey.Clear();
            #endregion

            if (grvTraining_Subject.GetFocusedRowCellValue(gclSubject_SameKey) != null)
            {
                gctdraft.DataSource =
                    m_DBMain.m_DCReadOnly.Training_Subjects.GetBySubjectSameKey(
                        Convert.ToInt16(
                        grvTraining_Subject.GetFocusedRowCellValue(gclSubject_SameKey).ToString()));
            }
            else
            {
                gctdraft.DataSource = 
                    m_DBMain.m_DCReadOnly.Training_Subjects.GetBySubjectKey(
                        Convert.ToInt16(
                        grvTraining_Subject.GetFocusedRowCellValue(gclSubjectKey).ToString()));
            }

            if (grvdraft.RowCount > 0)
            {
                for (int i = 0; i < grvdraft.RowCount; i++)
                {
                    m_ilistSubjectKey.Add(
                        Convert.ToInt16(grvdraft.GetRowCellValue(i, "Training_SubjectKey").ToString()));
                    DataTable dt = new DataTable();
                    dt = m_DBMain.m_DCReadOnly.Training_SubjectOpenRequests.GetProgramBySubjectKey(
                        m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details,
                        m_DBMain.m_DCReadOnly.Training_TrainingPrograms,
                        m_DBMain.m_DCReadOnly.Catalogue_Majors,
                        m_DBMain.m_DCReadOnly.Catalogue_Departments,
                        Convert.ToInt16(
                        grvdraft.GetRowCellValue(i,"Training_SubjectKey").ToString())).ToDataTable();
                    if (dt.Rows.Count > 0)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            Program.Rows.Add(
                                dt.Rows[j]["Training_TrainingProgramKey"],
                                dt.Rows[j]["ID"],
                                dt.Rows[j]["TrainingProgramName"],
                                dt.Rows[j]["Catalogue_DepartmentKey"],
                                dt.Rows[j]["Catalogue_CourseKey"],
                                dt.Rows[j]["Catalogue_LevelKey"],
                                dt.Rows[j]["Catalogue_MajorKey"],
                                dt.Rows[j]["Catalogue_TrainingTypeKey"]);
                        }
                    }
                    else
                    {
                    }
                }
            }
            else
            {
            }

            gctTraining_Program.DataSource = Program;
        }
        #endregion
    }
}